﻿@page "/catalog/products"
@attribute [MustHavePermission(FSHAction.View, FSHResource.Products)]

@inject IStringLocalizer<Products> L

<FshTitle Title="@L["Products"]" Description="@L["Manage Products."]" />

<EntityTable @ref="_table" TEntity="ProductDto" TId="Guid" TRequest="ProductViewModel" Context="@Context">

    <AdvancedSearchContent>
        <BrandAutocomplete @bind-Value="SearchBrandId" />
        <MudSlider T="decimal" @bind-value="SearchMinimumRate" Min="0" Max="9999" Step="100">@L["Minimum Rate"]: @_searchMinimumRate.ToString()</MudSlider>
        <MudSlider T="decimal" @bind-value="SearchMaximumRate" Min="0" Max="9999" Step="100">@L["Maximum Rate"]: @_searchMaximumRate.ToString()</MudSlider>
    </AdvancedSearchContent>

    <EditFormContent>
        @if (!Context.AddEditModal.IsCreate)
        {
            <MudItem xs="12" md="6">
                <MudTextField Value="context.Id" ReadOnly DisableUnderLine Label="@L[$"Product Id"]" />
            </MudItem>
        }
        <MudItem xs="12" md="6">
            <MudTextField Label="@L["Name"]" For="@(() => context.Name)" @bind-Value="context.Name" />
        </MudItem>
        <MudItem xs="12" md="6">
            <MudTextField Label="@L["Description"]" For="@(() => context.Description)"
                @bind-Value="context.Description" />
        </MudItem>
        <MudItem xs="12" md="6">
            <BrandAutocomplete For="@(() => context.BrandId)" @bind-Value="context.BrandId" OffsetY="true" />
        </MudItem>
        <MudItem xs="12" md="6">
            <MudTextField Label="@L["Rate"]" For="@(() => context.Rate)" @bind-Value="context.Rate" />
        </MudItem>
        <MudItem xs="12" md="12">
            <MudItem>
                @if(!string.IsNullOrEmpty(context.ImageInBytes))
                {
                    <MudAvatar Image ="@context.ImageInBytes" Square="true" Style="height: auto; width: auto; max-height: 100%"> </MudAvatar>
                }
                else
                {
                    <MudAvatar Image="@(string.IsNullOrEmpty(@context.ImagePath) ? string.Empty : (Config[ConfigNames.ApiBaseUrl] + @context.ImagePath))" Square="true" Style="height: auto; width: auto; max-height: 100%"> </MudAvatar>
                }
            </MudItem>
            <MudItem>
                <div>
                    <MudButton HtmlTag="label" DisableElevation Variant="Variant.Filled" Color="Color.Primary" Size="Size.Small"
                        StartIcon="@Icons.Filled.CloudUpload" for="fileInput">
                        @L["Upload"]
                    </MudButton>
                    @if(!Context.AddEditModal.IsCreate && !string.IsNullOrEmpty(context.ImagePath) && string.IsNullOrEmpty(context.ImageInBytes))
                    {
                        <MudButton HtmlTag="label" DisableElevation Variant="Variant.Filled" Color="Color.Info" Size="Size.Small" Target="_blank"
                            StartIcon="@Icons.Filled.RemoveRedEye" Link="@(Config[ConfigNames.ApiBaseUrl] + @context.ImagePath)" Style="margin-left: 5px;" >
                            @L["View"]
                        </MudButton>

                        <MudButton HtmlTag="label" DisableElevation Variant="Variant.Filled" Color="Color.Error" Size="Size.Small"
                            StartIcon="@Icons.Filled.Delete" OnClick="SetDeleteCurrentImageFlag" Style="margin-left: 5px;" >
                            @L["Delete"]
                        </MudButton>
                    }
                    @if(!string.IsNullOrEmpty(context.ImageInBytes))
                    {
                        <MudButton HtmlTag="label" DisableElevation OnClick="ClearImageInBytes" Variant="Variant.Filled" Color="Color.Warning" Size="Size.Small" Target="_blank"
                            StartIcon="@Icons.Filled.Clear" Style="margin-left: 5px;" >
                            @L["Clear"]
                        </MudButton>
                    }
                </div>
                <InputFile id="fileInput" hidden OnChange="UploadFiles" />
            </MudItem>
        </MudItem>
    </EditFormContent>

</EntityTable>